package cn.iocoder.ny.module.mall.service.impl;

import cn.iocoder.ny.module.mall.domain.MallConfig;
import cn.iocoder.ny.module.mall.domain.MallOrder;
import cn.iocoder.ny.module.mall.domain.MallProduct;
import cn.iocoder.ny.module.mall.mapper.MallOrderMapper;
import cn.iocoder.ny.module.mall.mapper.MallProductMapper;
import cn.iocoder.ny.module.mall.service.IMallOrderService;
import cn.iocoder.ny.module.mall.service.IMallProductService;
import cn.iocoder.ny.module.mall.service.IStatistcsService;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.module.system.dal.dataobject.system.SystemUsers;
import cn.iocoder.yudao.module.system.service.system.ISystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * @author luchunhong
 * @date 2024/11/13/ 15:18
 * @description:
 */
@Service
public class IStatistcsServiceImpl implements IStatistcsService {

    @Autowired
    private MallOrderMapper orderMapper;
    @Resource
    private ISystemUserService systemUserService;
    @Autowired
    private IMallOrderService mallOrderService;

    @Autowired
    private IMallProductService mallProductService;
    @Autowired
    private MallProductMapper productMapper;
    @Override
    public Map<String,Object> querySaleAndUserAndVerificationTotal() {
        LoginUser loginUser = getLoginUser();
        BigDecimal bigDecimal = orderMapper.querySaleTotal(loginUser.getTenantId());
        //统计总用户数
        Long userCount = systemUserService.lambdaQuery().eq(SystemUsers::getTenantId, loginUser.getTenantId())
                .eq(SystemUsers::getDeleted, 0).eq(SystemUsers::getStatus, 0).count();
        // 统计总订单数+待核销
        Long orderTotalCount = mallOrderService.lambdaQuery().eq(MallOrder::getDelFlag, 0).eq(MallOrder::getTenantId, loginUser.getTenantId()).count();
        // 统计待核销
        Long afterVerificationTotal = mallOrderService.lambdaQuery().eq(MallOrder::getStatus, 1)
                .eq(MallOrder::getTenantId, loginUser.getTenantId()).eq(MallOrder::getDelFlag, 0).count();
        // 统计最近30天收款趋势
        List<Map<String, Object>> queryMonthSaleTotal = orderMapper.selectMonthSaleTotal(loginUser.getTenantId());


        Map<String, Object> totalMap = new HashMap<>();
        totalMap.put("saleTotal",bigDecimal);
        totalMap.put("orderTotalCount",orderTotalCount);
        totalMap.put("afterVerificationTotal",afterVerificationTotal);
        totalMap.put("userCount",userCount);
        totalMap.put("monthSaleTotal",queryMonthSaleTotal);
        return totalMap;
    }

    @Override
    public Map<String, Object> queryProductStatistics() {
        LoginUser loginUser = getLoginUser();
        // 查询当前店铺有多少商品total
        Long productCount = mallProductService.lambdaQuery().eq(MallProduct::getTenantId, loginUser.getTenantId())
                .eq(MallProduct::getDelFlag, 0).count();
        // 查询分类销量
        List<HashMap<String, Object>> cateGorySaleDesc = productMapper.queryCategorySaleDesc(loginUser.getTenantId());
        Map<String, Object> productTotalMap = new HashMap<>();
        productTotalMap.put("cateGorySaleDesc",cateGorySaleDesc);
        productTotalMap.put("productCount",productCount);
        return productTotalMap;
    }

    @Override
    public Map<String, Object> queryProductSaleRank() {
        LoginUser loginUser = getLoginUser();
        List<HashMap<String, Object>> hashMaps = productMapper.queryProductSaleRank(loginUser.getTenantId());
        Map<String, Object> productSaleRankMap = new HashMap<>();
        productSaleRankMap.put("saleRank",hashMaps);
        return productSaleRankMap;
    }
}
